#include "StdAfx.h"
#include "RenderWindowForm.h"
#include "OgreInit.h"

using namespace particleEditorCli;

RenderWindowForm::RenderWindowForm(int nb):
											mRw(0),
											mCamera(0),
											mVp(0),
											mSceneMgr(0)											
{											
	InitializeComponent();					

	HWND hWnd = (HWND) (void*) mPictureBox->Handle.ToPointer();
	NameValuePairList misc;
	misc["externalWindowHandle"] = Ogre::StringConverter::toString((int)hWnd);

	Ogre::String camName = "SceneView//Cam" + Ogre::StringConverter::toString(nb);
	mRw = Ogre::Root::getSingletonPtr()->createRenderWindow(camName, 800, 600, false, &misc);
	
	mSceneMgr = OgreInit::getSingletonPtr()->getSceneManager();
	mCamera = mSceneMgr->createCamera(camName);
	mVp = mRw->addViewport(mCamera);
	mVp->setBackgroundColour(Ogre::ColourValue::White);
	mVp->setAutoUpdated(true);
	mCamera->setAutoAspectRatio(true);
	
	// create Target orbiting node
	mTargetNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	mTargetNode->setPosition(Vector3(0,0,0));
	mTargetNode->setFixedYawAxis(true);
	mTargetNode->lookAt(-mCamera->getPosition(),Ogre::SceneNode::TS_WORLD);

	// Create Camera node
	mCamNode = mTargetNode->createChildSceneNode();   

	// attach camera
	mCamNode->translate(Ogre::Vector3(0,0,-50));
	mCamNode->setFixedYawAxis(true);
	mCamNode->lookAt(Ogre::Vector3(0,0,0),Ogre::SceneNode::TS_PARENT);
	mCamNode->attachObject( mCamera );

	mVp->setBackgroundColour(ColourValue(0.0f,0.0f,0.0f,1.0f));//0.5f,0.5f,0.5f,1.0f));


	MouseWheel						+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseWheelCallback);
	mPictureBox->MouseLeave			+= gcnew EventHandler		(this, &RenderWindowForm::OnMouseLeaveCallback);
	mPictureBox->MouseEnter			+= gcnew EventHandler		(this, &RenderWindowForm::OnMouseEnterCallback);
	mPictureBox->MouseUp			+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseUpCallback);
	mPictureBox->MouseDown			+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseDownCallback);
	mPictureBox->MouseDoubleClick	+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseDoubleClickCallback);
	mPictureBox->MouseClick			+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseClickCallback);
	mPictureBox->MouseMove			+= gcnew MouseEventHandler	(this, &RenderWindowForm::OnMouseMoveCallback);

	mPictureBox->KeyDown			+= gcnew KeyEventHandler(this, &RenderWindowForm::OnKeyDownCallback);
	mPictureBox->KeyUp				+= gcnew KeyEventHandler(this, &RenderWindowForm::OnKeyUpCallback);
	mPictureBox->KeyPress			+= gcnew KeyPressEventHandler(this, &RenderWindowForm::OnKeyPressCallback);
	
	
}

RenderWindowForm::~RenderWindowForm()
{
	if (components)
	{
		delete components;
	}
}

void RenderWindowForm::InitializeComponent(void)
{
	this->mPictureBox = (gcnew System::Windows::Forms::PictureBox());
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mPictureBox))->BeginInit();
	this->SuspendLayout();
	// 
	// mPictureBox
	// 
	this->mPictureBox->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
	this->mPictureBox->Dock = System::Windows::Forms::DockStyle::Fill;
	this->mPictureBox->Location = System::Drawing::Point(0, 0);
	this->mPictureBox->Name = L"mPictureBox";
	this->mPictureBox->Size = System::Drawing::Size(284, 262);
	this->mPictureBox->TabIndex = 0;
	this->mPictureBox->TabStop = false;
	// 
	// RenderWindowForm
	// 
	this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
	this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
	this->ClientSize = System::Drawing::Size(284, 262);
	this->Controls->Add(this->mPictureBox);
	this->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
		static_cast<System::Byte>(0)));
	this->Name = L"RenderWindowForm";
	this->Text = L"RenderWindowForm";
	(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->mPictureBox))->EndInit();
	this->ResumeLayout(false);

}

void RenderWindowForm::OnMouseWheelCallback(Object^sender, MouseEventArgs^e)
{
	mCamera->moveRelative(Vector3(0,0,-e->Delta));
	mCamera->lookAt(Vector3(0,0,0));
	//mCamNode->tra->translate(Ogre::Vector3(0,0,-e->Delta));

}

void RenderWindowForm::OnMouseLeaveCallback(Object^sender, EventArgs^e)
{

}

void RenderWindowForm::OnMouseEnterCallback(Object^sender, EventArgs^e)
{

}

void RenderWindowForm::OnMouseUpCallback(Object^sender, MouseEventArgs^e)
{

}

void RenderWindowForm::OnMouseDownCallback(Object^sender, MouseEventArgs^e)
{
	mPrevMousePos = e->Location;
}

void RenderWindowForm::OnMouseDoubleClickCallback(Object^sender, MouseEventArgs^e)
{

}

void RenderWindowForm::OnMouseClickCallback(Object^sender, MouseEventArgs^e)
{

}

void RenderWindowForm::OnMouseMoveCallback(Object^sender, MouseEventArgs^e)
{
	if(e->Button == System::Windows::Forms::MouseButtons::Right)
	{
		Point diff = Point( e->Location.X- mPrevMousePos.X, e->Location.Y - mPrevMousePos.Y);
		
		mTargetNode->pitch(Ogre::Degree(-0.13 * diff.Y ), Ogre::SceneNode::TS_LOCAL);
		mTargetNode->yaw(Ogre::Degree(-0.13 * diff.X), Ogre::SceneNode::TS_WORLD);

		//mTargetNode->yaw(	-Degree(diff.X));
		//mTargetNode->pitch(	-Degree(diff.Y));
		//mTargetNode->rotate(Vector3(1,0,0),-Degree(diff.Y), Ogre::Node::TransformSpace::TS_WORLD);
		//mTargetNode->rotate(Vector3(0,1,0),-Degree(diff.X), Ogre::Node::TransformSpace::TS_WORLD);
		mPrevMousePos = e->Location;
	}
}

//keyboard events
void RenderWindowForm::OnKeyDownCallback(Object^sender, KeyEventArgs^e)
{

}

void RenderWindowForm::OnKeyUpCallback(Object^sender, KeyEventArgs^e)
{

}

void RenderWindowForm::OnKeyPressCallback(Object^sender, KeyPressEventArgs^e)
{

}
